package cn.wolfcode.rbac.web.advice;

import cn.wolfcode.rbac.domain.vo.R;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handlerException(Exception exception){
        exception.printStackTrace();
        return R.fail(exception.getMessage());
    }
    //授权失败
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public R handleException(AccessDeniedException exception){
        exception.printStackTrace();
        //授权异常处理
        return R.fail(HttpStatus.FORBIDDEN.value(),"没有权限访问");
    }
    // 认证失败
    @ExceptionHandler(BadCredentialsException.class)
    @ResponseBody
    public R handleException(BadCredentialsException exception){
        exception.printStackTrace();
        //授权异常处理
        return R.fail(HttpStatus.UNAUTHORIZED.value(),"认证失败,请求重新登录");
    }
}
